﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonSense.UI.MVVM.ViewModels;
using CommonSense.UI.Shell.ViewModels;
using CommonSense.UI.Shell.Interfaces;

namespace CommonSense.UI.Shell.Services
{
    class WorkspaceService : IWorkspaceService
    {
        private IModuleService moduleService;
        private RootViewModel rootViewModel;

        public event EventHandler ActiveViewModelDirtyChanged
        {
            add { this.rootViewModel.SelectedItemDirtyChanged += value; }
            remove { this.rootViewModel.SelectedItemDirtyChanged -= value; }
        }

        public event EventHandler ActiveViewModelChanged
        {
            add { this.rootViewModel.SelectedItemChanged += value; }
            remove { this.rootViewModel.SelectedItemChanged -= value; }
        }

        public BaseViewModel ActiveViewModel
        {
            get { return rootViewModel.SelectedItem; }
        }

        public WorkspaceService(IModuleService moduleService, RootViewModel rootViewModel)
        {
            this.moduleService = moduleService;
            this.moduleService.DIContainer.RegisterSingletonInstance<IWorkspaceService>(this);

            this.rootViewModel = rootViewModel;
        }

        public void OpenView<TViewModel>(TViewModel viewModel) where TViewModel : BaseViewModel
        {
            this.rootViewModel.OpenItem(viewModel);
        }
    }
}
